<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Invoice_line_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $invoice_id = '';
	var $product_ref = 0;
	var $type = 0;
	var $quantity = '';
	var $price = 0;
	var $tax = '';
	var $description = '';
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setInvoiceId($input){
		$this->invoice_id = $input;
	}
	function setProductRef($input){
		$this->product_ref = $input;
	}
	function setType($input){
		$this->type = $input;
	}
	function setQuantity($input){
		$this->quantity = $input;
	}
	function setPrice($input){
		$this->price = $input;
	}
	function setTax($input){
		$this->tax = $input;
	}
	function setDescription($input){
		$this->description = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getInvoiceId(){
		return $this->invoice_id;
	}
	function getProductRef(){
		return $this->product_ref;
	}
	function getType(){
		return $this->type;
	}
	function getQuantity(){
		return $this->quantity;
	}
	function getPrice(){
		return $this->price;
	}
	function getTax(){
		return $this->tax;
	}
	function getDescription(){
		return $this->description;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_INVOICE_LINE)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->invoice_id = $result[0]->invoice_id;
			$this->product_ref = $result[0]->product_ref;
			$this->type = $result[0]->type;
			$this->quantity = $result[0]->quantity;
			$this->price = $result[0]->price;
			$this->tax = $result[0]->tax;
			$this->description = $result[0]->description;
			return $this;
		}
		return false;
	}
	
	function getListByInvoice($invoice_id){
		$ci=& get_instance();
		$ci->db->where('invoice_id', $invoice_id);
		return $ci->db->get(TBL_INVOICE_LINE)->result();
	}
	function count(){
		$ci=& get_instance();
		return $ci->db->count_all_results(TBL_INVOICE_LINE);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set('invoice_id',$this->getInvoiceId());
		$ci->db->set('product_ref',$this->getProductRef());
		$ci->db->set('type',$this->getType());
		$ci->db->set('quantity',$this->getQuantity());
		$ci->db->set('price',$this->getPrice());
		$ci->db->set('tax',$this->getTax());
		$ci->db->set('description',$this->getDescription());
		$ci->db->insert(TBL_INVOICE_LINE);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('invoice_id',$this->getInvoiceId());
		$ci->db->set('product_ref',$this->getProductRef());
		$ci->db->set('type',$this->getType());
		$ci->db->set('quantity',$this->getQuantity());
		$ci->db->set('price',$this->getPrice());
		$ci->db->set('tax',$this->getTax());
		$ci->db->set('description',$this->getDescription());
		return $ci->db->where('id', $this->id)->update(TBL_INVOICE_LINE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_INVOICE_LINE);
	}
}